<?php
namespace app\admin\controller;
use think\Db;
class Admin extends Base{
    public function adminList(){
        $key=input('get.key');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
	        $list=Db::name('admin')->alias('a')->join('adminrole b', 'a.role_id = b.id','LEFT')->field('a.*,b.title')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('username','like',"%{$key}%")->order('id desc')->paginate(10);
	        $lists=Db::name('admin')->alias('a')->join('adminrole b', 'a.role_id = b.id','LEFT')->field('a.*,b.title')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('username','like',"%{$key}%")->select();
        
        $this->assign('key',input('get.key'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    public function state_off(){
    	$id=input('param.id');
        $status = Db::name('admin')->where(array('id'=>$id))->update(['status'=>2]);
        if($status){
            return json(['code' => 200, 'data' =>$status, 'msg' => '已禁止']);
        }else{
            return json(['code' => 500, 'data' =>$status, 'msg' => '出错']);
        }
    }
    public function state_on(){
    	$id=input('param.id');
        $status = Db::name('admin')->where(array('id'=>$id))->update(['status'=>1]);
        if($status){
            return json(['code' => 200, 'data' =>$status, 'msg' => '已开启']);
        }else{
            return json(['code' => 500, 'data' =>$status, 'msg' => '出错']);
        }
    }
    public function admin_add(){
    	if(request()->isGet()){
            $role=Db::name('adminrole')->where('status',1)->order('id asc')->select();
            $this->assign('role',$role);
    		return $this->fetch();
    	}elseif(request()->isPost()){
			$param = input('post.');
			$data['username']=$param['username'];
			$data['password']=md5('tmd'.$param['password']);
			$data['role_id']=$param['adminRole'];
			$data['time']=time();
			if($param['status']='on'){
				$data['status']=1;
			}elseif($param['status']='off'){
				$data['status']=2;
			}
			$res=Db::name('admin')->insert($data);
            return json(['code' => 200, 'data' =>$res, 'msg' => '调试']);
    	}
    }
    public function admin_edit(){
    	if(request()->isGet()){
			$param = input('get.');
			$user=Db::name('admin')->where('id',$param['id'])->find();
            $role=Db::name('adminrole')->where('status',1)->order('id asc')->select();
            $this->assign('role',$role);
	        $this->assign('user',$user);
    		return $this->fetch();
    	}elseif(request()->isPost()){
			$param = input('post.');
			$data['username']=$param['username'];
			$data['password']=md5('tmd'.$param['password']);
			$data['role_id']=$param['adminRole'];
			$data['time']=time();
			if($param['status']='on'){
				$data['status']=1;
			}elseif($param['status']='off'){
				$data['status']=2;
			}
        	$res= Db::name('admin')->where(array('id'=>$param['id']))->update($data);
            return json(['code' => 200, 'data' =>$res, 'msg' => '更新成功']);
    	}
    }
    public function admin_del(){
		$param = input('post.');
        $res= Db::name('admin')->where(array('id'=>$param['id']))->delete();
        return json(['code' => 200, 'data' =>$res, 'msg' => '删除成功']);

    }
    public function admin_dels(){
		$param = input('post.');
        $res= Db::name('admin')->where(array('id'=>$param['id']))->delete();
        return json(['code' => 200, 'data' =>$res, 'msg' => '删除成功']);

    }
}
